<template>
  <div v-if="list.length">
    <div class="list-item" v-for="(item,index) in list" :key="index">
      <img class="list-img" :src='imglist[index % 5]' alt="">
      <!-- <img class="list-img" src='../assets/1.png' alt=""> -->
      <div class="list-right">
        <p class="list-title">{{item.trainCourseName}}<span class="ending" v-if="item.isEnd">已结束</span></p>
        <p class="list-cont">{{item.trainCourseIntroduction}}</p>
        <p class="list-time">培训时间：{{item.trainStartTime | timeFormat("YYYY/MM/DD ")}} - {{item.trainEndTime | timeFormat("MM/DD ")}}</p>
        <button v-if="!item.isEnd " class="apply" :class="allList.includes(item.id)?'apply1':''" @click="applyClick(item)" :disabled="allList.includes(item.id)" >  {{allLists.includes(item.id)?"审核未通过":allList.includes(item.id)?"审核通过":"报名"}}    <img src="../assets/arrow.png"
            class="arrow-right" alt=""></button>
      </div>
    </div>
    <button class="check-more" @click="clickMore">查看更多<img src="../assets/arrow.png" class="arrow-right" alt=""></button>
  </div>
</template>

<script>
  import {
    insertEnroll,selectEnrolledUser,selectNoEnrolledUser
  } from "@/api/apply"
  import {
    mapState,
    mapMutations
  } from "vuex"
  export default {
    props: {
      list: {
        type: Array,
        default: () => []
      },
      title: {
        type: String,
        default: ""
      }
    },
    computed: {
      ...mapState(['showDialog'])
    },
    data() {
      return {
        allLists:[],
        allList:[],
        flag: false,
        baseUrl: "http://59.173.18.239:2051/group1/M00/00/00/",
        imglist: ["wKiJXF-QHDqEYyaxAAAAAAAAAAA170.png", "wKiJXF-QN52EERN9AAAAAAAAAAA151.png",
          "wKiJXF-QN62ESaGiAAAAAAAAAAA956.png", "wKiJXF-QN6mET3l1AAAAAAAAAAA226.png",
          "wKiJXF-QN6OEYWgoAAAAAAAAAAA082.png"
        ],
        dateTime: 0
      }
    },
    methods: {
      ...mapMutations(['changeDialog']),
      add0(m) {
        return m < 10 ? '0' + m : m
      },
      timeFormat() {
        var time = new Date();
        var year = time.getFullYear();
        var month = time.getMonth() + 1;
        var date = time.getDate();
        var hours = time.getHours();
        var minutes = time.getMinutes();
        var seconds = time.getSeconds();
        return year + '-' + this.add0(month) + '-' + this.add0(date) + ' ' + this.add0(hours) + ':' + this.add0(minutes) +
          ':' + this.add0(
            seconds);
      },
      applyClick(item) {
        if (sessionStorage.getItem("name")){
           let time = this.timeFormat();
        let personid = sessionStorage.getItem("id");
        let personname = sessionStorage.getItem("name");
        let number = sessionStorage.getItem("jobNumber")
        const params = [{
          trainId: item.trainId,
          trainPlanId: item.id,
          trainName: item.trainCourseName,
          status: 0,
          createTime: time,
          jobNumber:number
        }]
        this.$confirm('确认报名吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(() => {
          insertEnroll(params).then(res => {
            if (res.code == 410) {
              this.$message({
                message: "请登录后报名",
                type: 'info'
              });
              this.changeDialog(true);
            } else if (res.code == 200) {
              this.$message({
                message: res.message,
                type: 'success'
              });
            } else if (res.code == 501) {
              this.$message({
                message: res.message,
                type: 'warning'
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
        }else{
          this.$message({
                message: "请登录后报名",
                type: 'info'
              });
          this.changeDialog(true);
        }
       
      },
      clickMore() {
        this.$emit("clickmore")
      }
    },
    created() {
      for (let i = 0; i < this.imglist.length; i++) {
        this.imglist[i] = this.baseUrl + this.imglist[i]
      }  
    },
    mounted(){
      if(sessionStorage.getItem("jobNumber")){
        var jobNumber = sessionStorage.getItem("jobNumber")
      // console.log(jobNumber);
      selectEnrolledUser({jobNumber}).then(res=>{
          this.allList = res.result
        })
        selectNoEnrolledUser({jobNumber}).then(res=>{
        this.allLists = res.result
      })
      }else{
        this.allList=""
      }
      
    },
    
  }
</script>

<style scoped="scoped">
  .list-item {
    display: flex;
    width: 1200px;
    height: 227px;
    padding: 20px 0;
    border-bottom: solid 1px #E4E4E4;
    box-sizing: border-box;
  }

  .list-img {
    width: 250px;
    height: 187px;
    margin-right: 40px;
  }

  .list-title {
    font-size: 20px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    line-height: 24px;
    color: #106470;
    display: flex;
    align-items: center;
  }

  .list-cont {
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    line-height: 24px;
    color: #666666;
    margin-top: 20px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  .list-time {
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    line-height: 24px;
    color: #999999;
    margin-top: 15px;
  }

  .check-more {
    width: 118px;
    height: 32px;
    background: #000000;
    border: none;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    line-height: 32px;
    color: #FFFFFF;
    margin-top: 30px;
  }

  .arrow-right {
    width: 6px;
    height: 11px;
    margin-left: 12px;
  }

  .ending {
    display: block;
    width: 56px;
    height: 24px;
    background: #E9E9E9;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    line-height: 24px;
    color: #999999;
    text-align: center;
    margin-left: 15px;
  }

  .list-right {
    position: relative;
  }

  .apply {
    width: 118px;
    height: 32px;
    background: #000000;
    border: none;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    line-height: 32px;
    color: #FFFFFF;
    position: absolute;
    bottom: 0;
  }

  .apply1{
    background: green;
  }
</style>
